package com.dormitoryAdministratorSystem.service.room.impl;

import com.dormitoryAdministratorSystem.dao.room.impl.RoomDaoImpl;
import com.dormitoryAdministratorSystem.entity.page.PageBean;
import com.dormitoryAdministratorSystem.entity.room.RoomBean;
import com.dormitoryAdministratorSystem.service.room.IRoomService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RoomSerImpl implements IRoomService {

   private RoomDaoImpl rdi = new RoomDaoImpl();

    @Override
    public List<RoomBean> getAllCanLiveRoom() {
        return rdi.getCanLiveRoom();
    }

    @Override
    public int updateRoomInfo(int rId, int peopleNo) {
        return rdi.updateRoomInfo(rId,peopleNo);
    }

    @Override
    public PageBean<RoomBean> showRoomInfos(Map<String, String> con) {
        PageBean<RoomBean> p = new PageBean<>();
        List<String> list = new ArrayList<>();
        String sql = "select rid, address, size, peopleNo, rent, roomState, hostName, hostTel, roomType, houseType, payMode, rentTime" +
                " from t_room where 1 = 1 ";
        String countSql = "select count(*) as rNum from t_room where 1 = 1 ";

        if(!"".equals(con.get("address")) && con.get("address") != null){
            sql += " and address like ? ";
            countSql += " and address like ? ";
            list.add("%"+con.get("address")+"%");
        }

        if(!"".equals(con.get("state")) && con.get("state") != null){
            sql += " and roomState like ? ";
            countSql += " and roomState like ? ";
            list.add("%"+con.get( "state") + "%");
        }

        if(!"".equals(con.get("type")) && con.get("type") != null){
            sql += " and roomType like ? ";
            countSql += " and roomType like ?  ";
            list.add("%" + con.get("type") + "%");
        }

        if(!"".equals(con.get("canLive")) && con.get("canLive") != null){
            sql += " and size > peopleNo ";
            countSql += " and size > peopleNo  ";
//            list.add(con.get("canLive"));
        }

        if("".equals(con.get("curPage")) || con.get("curPage") == null){
            p.setCurPage(1);
        }else{
            p.setCurPage(Integer.parseInt(con.get("curPage")));
        }

        if("".equals(con.get("pageSize")) || con.get("pageSize") == null){
            p.setPageSize(5);
        }else{
            p.setPageSize(Integer.parseInt(con.get("pageSize")));
        }

        Object[] parms1 = new Object[list.size()];
        Object[] parms2 = new Object[list.size() + 2];

        for (int i = 0; i < list.size(); i++) {
            parms1[i] = list.get(i);
            parms2[i] = list.get(i);
        }

        p.setTolNum(rdi.getRoomTolNum(countSql,parms1));
        p.setTolSize((int) Math.ceil((double)p.getTolNum() / p.getPageSize()));

        sql += " order by rid desc limit ?,?";
//        System.out.println(sql);
        parms2[parms1.length] = (p.getCurPage() - 1) * p.getPageSize();
        parms2[parms1.length + 1] = p.getPageSize();
        p.setDates(rdi.getRoomByPage(sql,parms2));

        return p;
    }

    @Override
    public int addRoomInfo(RoomBean newRoom) {
        return rdi.addRoomInfo(newRoom);
    }

    @Override
    public int deleteRoom(int rId) {
        return rdi.deleteRoom(rId);
    }

    @Override
    public RoomBean getRoomInfo(int rId) {
        return rdi.getRoomInfo(rId);
    }

    @Override
    public int changeRoomState(String state, int id) {
        return rdi.changeRoomState(state, id);
    }

    @Override
    public List<RoomBean> getAllRoom() {
        return rdi.getAllRooms();
    }
}
